package org.edu.modules.message.controller;

import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.edu.common.api.vo.Result;
import org.edu.common.aspect.annotation.AutoLog;
import org.edu.common.system.base.controller.BaseController;
import org.edu.common.system.query.QueryGenerator;
import org.edu.modules.message.entity.VeMsgSend;
import org.edu.modules.message.service.IVeMsgSendService;
import org.edu.modules.message.vo.MsgSendCountVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
* @Description: 已发消息
* @Author: jeecg-boot
* @Date:   2021-05-07
* @Version: V1.0
*/
@Api(tags="已发消息")
@RestController
@RequestMapping("/message/veMsgSend")
@Slf4j
public class VeMsgSendController extends BaseController<VeMsgSend, IVeMsgSendService> {
   @Autowired
   private IVeMsgSendService veMsgSendService;

   /**
    * 分页列表查询
    *
    * @param veMsgSend
    * @param pageNo
    * @param pageSize
    * @param req
    * @return
    */
   @AutoLog(value = "已发消息-分页列表查询")
   @ApiOperation(value="已发消息-分页列表查询", notes="已发消息-分页列表查询")
   @GetMapping(value = "/list")
   public Result<?> queryPageList(VeMsgSend veMsgSend,
                                  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                  HttpServletRequest req) {
       QueryWrapper<VeMsgSend> queryWrapper = QueryGenerator.initQueryWrapper(veMsgSend, req.getParameterMap());
       queryWrapper.orderByDesc("create_time");
       Page<VeMsgSend> page = new Page<VeMsgSend>(pageNo, pageSize);
       IPage<VeMsgSend> pageList = veMsgSendService.page(page, queryWrapper);
       return Result.OK(pageList);
   }

   /**
    *   添加
    *
    * @param veMsgSend
    * @return
    */
   @AutoLog(value = "已发消息-添加")
   @ApiOperation(value="已发消息-添加", notes="已发消息-添加")
   @PostMapping(value = "/add")
   public Result<?> add(@RequestBody VeMsgSend veMsgSend) {
       veMsgSendService.save(veMsgSend);
       return Result.OK("添加成功！");
   }

   /**
    *  编辑
    *
    * @param veMsgSend
    * @return
    */
   @AutoLog(value = "已发消息-编辑")
   @ApiOperation(value="已发消息-编辑", notes="已发消息-编辑")
   @PutMapping(value = "/edit")
   public Result<?> edit(@RequestBody VeMsgSend veMsgSend) {
       veMsgSendService.updateById(veMsgSend);
       return Result.OK("编辑成功!");
   }

   /**
    *   通过id删除
    *
    * @param id
    * @return
    */
   @AutoLog(value = "已发消息-通过id删除")
   @ApiOperation(value="已发消息-通过id删除", notes="已发消息-通过id删除")
   @DeleteMapping(value = "/delete")
   public Result<?> delete(@RequestParam(name="id",required=true) String id) {
       veMsgSendService.removeById(id);
       return Result.OK("删除成功!");
   }

   /**
    *  批量删除
    *
    * @param ids
    * @return
    */
   @AutoLog(value = "已发消息-批量删除")
   @ApiOperation(value="已发消息-批量删除", notes="已发消息-批量删除")
   @DeleteMapping(value = "/deleteBatch")
   public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
       this.veMsgSendService.removeByIds(Arrays.asList(ids.split(",")));
       return Result.OK("批量删除成功!");
   }

   /**
    * 通过id查询
    *
    * @param id
    * @return
    */
   @AutoLog(value = "已发消息-通过id查询")
   @ApiOperation(value="已发消息-通过id查询", notes="已发消息-通过id查询")
   @GetMapping(value = "/queryById")
   public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
       VeMsgSend veMsgSend = veMsgSendService.getById(id);
       if(veMsgSend==null) {
           return Result.error("未找到对应数据");
       }
       return Result.OK(veMsgSend);
   }

   /**
   * 导出excel
   *
   * @param request
   * @param veMsgSend
   */
   @RequestMapping(value = "/exportXls")
   public ModelAndView exportXls(HttpServletRequest request, VeMsgSend veMsgSend) {
       return super.exportXls(request, veMsgSend, VeMsgSend.class, "已发消息");
   }

   /**
     * 通过excel导入数据
   *
   * @param request
   * @param response
   * @return
   */
   @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
   public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
       return super.importExcel(request, response, VeMsgSend.class);
   }

    /**
     * 根据发送通道统计
     * @return
     */
    @AutoLog(value = "根据发送通道统计")
    @ApiOperation(value="根据发送通道统计", notes="根据发送通道统计")
    @GetMapping(value = "/sendCountByChannel")
    public Result<?> sendCountByChannel() {
        List<MsgSendCountVo> msgSendCountVos = veMsgSendService.getCountByChannel();
        return Result.OK(JSON.toJSON(msgSendCountVos));
    }
    /**
     * 根据发送用户统计
     * @return
     */
    @AutoLog(value = "根据发送用户统计")
    @ApiOperation(value="根据发送用户统计", notes="根据发送用户统计")
    @GetMapping(value = "/sendCountByUser")
    public Result<?> sendCountByUser(@RequestParam(name="channel",required=true) Integer channel) {
        List<MsgSendCountVo> msgSendCountVos = veMsgSendService.getCountByUser(channel);
        return Result.OK(JSON.toJSON(msgSendCountVos));
    }

}
